<?php
class Default_Service_Index
{
	/**
	* 获得用户登录信息
	*
	*/
	public function getLoginInfo(){
		$customServiceObj = new Merchant_Service_Customer();
		return $customServiceObj->getLoginInfo();
	}
	
	public static function getInstance(){
		return new Default_Service_Index();
	}
	
	public function validEmail($email){
		$cusServiceObj = Merchant_Service_Customer::getInstance();
		$emailError = $cusServiceObj->existsEmail($email);
		if(!$cusServiceObj->existsEmail($email)) return "不存在该User ID";
		return true;
	}
	
	/**
	* 检查注册用户留言数据的有效性
	*/
	public function checkRegisteredMessageData($customer_id,$message){
		$error = "";
//		$emailError = $this->validEmail($email);
//		if($emailError !== true) $error[] = $emailError;
		if("" == $customer_id){
			$error[] = "User ID can not be empty!";
		}
		if("" == $message){
			$error[] = "Message can not be empty!";
		}		
		/*
		if(is_array($error)) {
			return $error;
		}		
		return true;
		*/
		return $error;
	}
	
	/**
	* 检查非注册用户留言数据的有效性
	*/
	public function checkUnregisteredMessageData($row){
		$error = array();
		if("" == $row["first_name"]){
			$error[] = "First name can not be empty!";
		}
		if("" == $row["last_name"]){
			$error[] = "Last name can not be empty!";
		}
		if("" == $row['email']) {
			$error[] = "Email can not be empty!";
		}elseif(!preg_match("/^[0-9a-zA-Z]+(?:[\_\-][a-z0-9\-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\.[a-zA-Z]+$/i",$row['email'])){
			$error[] = "Email format is not correct!";
		}
		if("" == $row['phone']) {
			$error[] = "Phone can not be empty!";
		}elseif(!preg_match("/^[0-9]{1,15}$/i",$row['phone'])){
			$error[] = "Phone format is not correct!";
		}
		if("" == $row['message']) {
			$error[] = "Message can not be empty!";
		}		
		return $error;
	}
	
	
	/**
	* 注册用户添加留言
	* @param array $row
	* @param int $type
	* @return boolean
	*/
	public function addMessage($row,$type){
		if($type == 1){//注册用户
			//通过email查询
//			$cusServiceObj = Merchant_Service_Customer::getInstance();
//			$customer = $cusServiceObj->existsEmail($row['email']);
//			$insertRow = array(
//				"first_name" => $customer['customer_firstname'],
//				"last_name" => $customer['customer_lastname'],
//				"email" => $customer['customer_email'],
//				"message_type" => $type,
//				"phone" => $customer['customer_telephone'],
//				"message" => $row['message'],
//				"create_date" => date("Y-m-d H:i:s"),
//			);
			if(self::messageSendEmail($row['email'],$row['message'])){
				$res = Default_Model_Message::getInstance()->getDbTable()->insert($row);
				return $res;
			}
		}
		
		if($type == 0){//非注册用户
			if(self::messageSendEmail($row['email'],$row['message'])){
				$res = Default_Model_Message::getInstance()->getDbTable()->insert($row);			
				return $res;
			}
		}
	}
	
	public function messageSendEmail($email,$message){
		 $mailObj = Cff::setupMail();
         $emailTo = 'zqsu@sentfromchina.com';
         $emailCustomerName = $email;
         $subject = 'Message From Contact US Of Chinafullfill';
         $body = $message;
   
         $mailObj->addTo($emailTo, $emailCustomerName);
         $mailObj->setSubject($subject);
         $mailObj->setBodyHtml($body);
         $res = $mailObj->send();
		 if(!$res){
			return false;
		 }
		 return true;
	}
}